﻿using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using Library.Models;

namespace Library
{
    public static class Settings
    {        
        private static Dictionary<string, string> _settings;

        public static string DefaultCulture = ConfigurationManager.AppSettings["DefaultCulture"].ToLower();
        public static string LogDirectory = ConfigurationManager.AppSettings["LogDirectory"];
        public static string RootAdminRoleId = ConfigurationManager.AppSettings["RootAdminRoleId"];
        public static string RootAdminRoleName = ConfigurationManager.AppSettings["RootAdminRoleName"];
        public static string DefaultCurrency = ConfigurationManager.AppSettings["DefaultCurrency"];
        public static string ProductImageDiretories = ConfigurationManager.AppSettings["ProductImageDiretories"];
        public static string PageImageDiretories = ConfigurationManager.AppSettings["PageImageDiretories"];
        
        public static string[] ValidImageTypes = {
                        "image/gif",
                        "image/jpeg",
                        "image/pjpeg",
                        "image/png"
                    };

        public static void RegisterSettings()
        {
            using(var db = new EcommerceEntities())
            {
                _settings = db.Settings.ToDictionary(s => s.Key.Trim(), s => s.Value.Trim());
            }
        }

        public static string GetSetting(string key)
        {
            string value;
            if (_settings.TryGetValue(key, out value))
            {
                return value;
            }
            return null;
        }

        public static bool UpdateSetting(string key, string value)
        {
            if (string.IsNullOrEmpty(key))
                return false;

            var oldValue = GetSetting(key);
            if (oldValue != null)
            {
                _settings.Remove(key);
            }
            _settings.Add(key, value);

            // Update to database
            using (var db = new EcommerceEntities())
            {
                var setting = db.Settings.SingleOrDefault(i => i.Key.Equals(key));
                if (setting != null)
                {
                    setting.Value = value;
                }
                else
                {
                    setting = new Setting {Key = key, Value = value};
                    db.Settings.Add(setting);
                }
               
                return db.SaveChanges() > 0;
            }
        }

        public static bool UpdateSettings(Dictionary<string, string> newSettings)
        {
            if (newSettings == null)
                return false;

            using (var db = new EcommerceEntities())
            {                
                foreach (var newSetting in newSettings)
                {
                    if (string.IsNullOrEmpty(newSetting.Key))
                        continue;

                    // Update on system
                    var oldValue = GetSetting(newSetting.Key);
                    if (oldValue != null)
                    {
                        _settings.Remove(newSetting.Key);
                    }
                    _settings.Add(newSetting.Key, newSetting.Value);

                    // Update to database
                    var setting = db.Settings.SingleOrDefault(i => i.Key.Equals(newSetting.Key));
                    if (setting != null)
                    {
                        setting.Value = newSetting.Value;
                    }
                    else
                    {
                        setting = new Setting { Key = newSetting.Key, Value = newSetting.Value };
                        db.Settings.Add(setting);
                    }
                }

                return db.SaveChanges() > 0;
            }
        }
    }
}
